package org.aileen.blog.service;

import org.aileen.blog.mapper.BlogInfoMapper;
import org.aileen.blog.mapper.UserInfoMapper;
import org.aileen.blog.model.BlogInfo;
import org.aileen.blog.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo selectUserInfoByName(String userName) {
        return userInfoMapper.selectByName(userName);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectById(userId);
    }

    public UserInfo selectAuthorInfoByBlogId(Integer blogId) {
        //1.先根据blogId查询出作者的userId
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        //校验userId是否存在
        if(blogInfo == null || blogInfo.getUserId() == null) {
            return null; // 如果没有找到对应的博客信息，返回null
        }
        //2.根据userId查询出作者信息
        UserInfo userInfo = userInfoMapper.selectById(blogInfo.getUserId());
        return userInfo;
    }
}
